package com.example.homework8;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.homework8.fragment.MoneyFragment;
import com.example.homework8.fragment.PlanFragment;
import com.example.homework8.fragment.RecordEditFragment;
import com.example.homework8.fragment.CateFragment;
import com.example.homework8.fragment.MainFragment;
import com.example.homework8.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    FragmentManager fm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getSupportFragmentManager();

        setDefaultFragment();

        BottomNavigationView navigationView = findViewById(R.id.navig);
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                FragmentTransaction transaction = fm.beginTransaction();
                 switch (item.getItemId()){
                    case R.id.item_bottom_home :
                        MainFragment mainFragment  = new MainFragment();
                        transaction.replace(R.id.id_content, mainFragment);
                        transaction.commit();
                        break;
                    case R.id.item_bottom_plan :
                        PlanFragment planFragment  = new PlanFragment();
                        transaction.replace(R.id.id_content, planFragment);
                        transaction.commit();
                        break;
                    case R.id.item_bottom_money :
                        MoneyFragment moneyFragment  = new MoneyFragment();
                        transaction.replace(R.id.id_content, moneyFragment);
                        transaction.commit();
                        break;

                    case R.id.item_bottom_mine :
                        MeFragment fragment  = new MeFragment();
                        transaction.replace(R.id.id_content, fragment);
                        transaction.commit();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
    private void setDefaultFragment()
    {
        FragmentTransaction transaction = fm.beginTransaction();
        MainFragment mainFragment  = new MainFragment();
        transaction.replace(R.id.id_content, mainFragment);
        transaction.commit();
    }
}